﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WaterBill.BUS;
using DevExpress.XtraReports.UI;

namespace WaterBill.GUI
{
    public partial class frmExportBill : DevExpress.XtraEditors.XtraForm
    {
        BUS_Bills busBill = new BUS_Bills();
        BUS_Charges busCharge = new BUS_Charges();
        public frmExportBill()
        {
            InitializeComponent();
        }

        private void frmExportBill_Load(object sender, EventArgs e)
        {
            loadDataForm();
        }

        private void loadDataForm()
        {
            DataTable dtCharge = busCharge.BUS_Charge_GetByMaxId();
            if (dtCharge.Rows.Count > 0)
            {
                this.lblNameCharge.Text = dtCharge.Rows[0]["NameCharge"].ToString();
                this.txtChargeId.Text = dtCharge.Rows[0]["ChargeId"].ToString();           
                int ChargeId = Convert.ToInt32(this.txtChargeId.Text);
                gcBill.DataSource = busBill.BUS_Bills_GetByChargeId(ChargeId);
                this.gvBill.SelectAll();
            }      
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                if (gvBill.SelectedRowsCount > 0)
                {
                    DataSet dsBill = new DataSet();
                    DataTable dtBill = new DataTable();
                    dtBill.Columns.Add("FullName", typeof(string));
                    dtBill.Columns.Add("Address", typeof(string));
                    dtBill.Columns.Add("Phone", typeof(string));
                    dtBill.Columns.Add("BillNumber", typeof(string));
                    dtBill.Columns.Add("NameCharge", typeof(string));
                    dtBill.Columns.Add("OldIndex", typeof(int));
                    dtBill.Columns.Add("NewIndex", typeof(int));
                    dtBill.Columns.Add("UsedIndex", typeof(int));
                    dtBill.Columns.Add("Price", typeof(decimal));
                    dtBill.Columns.Add("TotalPrice", typeof(decimal));
                    DataRow dr;
                    foreach (int i in gvBill.GetSelectedRows())
                    {
                        if (gvBill.GetRowCellValue(i, "NewIndex").ToString() == "")
                        {
                            XtraMessageBox.Show("Cảnh báo, Có một số hóa đơn chưa được nhập chỉ số mới nên quá trình In không thể thực hiện.", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            frmAddBill frm = new frmAddBill();
                            frm.ShowDialog();
                            return;
                        }
                        dr = dtBill.NewRow();
                        dr = (DataRow)gvBill.GetDataRow(i);
                        dtBill.ImportRow(dr);
                    }
                    dsBill.Tables.Add(dtBill);
                    rptBillExport rptBill = new rptBillExport();
                    rptBill.DataSource = dsBill;
                    rptBill.DataMember = dsBill.Tables[0].TableName;
                    rptBill.lblFullName.DataBindings.Add("Text", null, "FullName");
                    rptBill.lblAddress.DataBindings.Add("Text", null, "Address");
                    rptBill.lblPhone.DataBindings.Add("Text", null, "Phone");
                    rptBill.lblBillNumber.DataBindings.Add("Text", null, "BillNumber");
                    rptBill.lblNameCharge.DataBindings.Add("Text", null, "NameCharge");
                    rptBill.lblOldIndex.DataBindings.Add("Text", null, "OldIndex");
                    rptBill.lblNewIndex.DataBindings.Add("Text", null, "NewIndex");
                    rptBill.lblUsedIndex.DataBindings.Add("Text", null, "UsedIndex");
                    rptBill.lblPrice.DataBindings.Add("Text", null, "Price", "{0:0,0 vnđ}");
                    rptBill.lblTotalPrice.DataBindings.Add("Text", null, "TotalPrice", "{0:0,0 vnđ}");
                    rptBill.lblPriceTotal.DataBindings.Add("Text", null, "TotalPrice", "{0:0,0 vnđ}");

                    rptBill.lblFullName2.DataBindings.Add("Text", null, "FullName");
                    rptBill.lblAddress2.DataBindings.Add("Text", null, "Address");
                    rptBill.lblPhone2.DataBindings.Add("Text", null, "Phone");
                    rptBill.lblBillNumber2.DataBindings.Add("Text", null, "BillNumber");
                    rptBill.lblNameCharge2.DataBindings.Add("Text", null, "NameCharge");
                    rptBill.lblOldIndex2.DataBindings.Add("Text", null, "OldIndex");
                    rptBill.lblNewIndex2.DataBindings.Add("Text", null, "NewIndex");
                    rptBill.lblUsedIndex2.DataBindings.Add("Text", null, "UsedIndex");
                    rptBill.lblPrice2.DataBindings.Add("Text", null, "Price", "{0:0,0 vnđ}");
                    rptBill.lblTotalPrice2.DataBindings.Add("Text", null, "TotalPrice", "{0:0,0 vnđ}");
                    rptBill.lblPriceTotal2.DataBindings.Add("Text", null, "TotalPrice", "{0:0,0 vnđ}");

                    //rptBill.ShowPreview();
                    rptBill.PrintDialog();
                }
                else
                {
                    XtraMessageBox.Show("Cảnh báo, Không có dòng nào được chọn.", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch
            {
                XtraMessageBox.Show("Lỗi, đã xảy ra lỗi không xác định được. Xin vui lòng thử lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }       
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (txtChargeId.Text != "")
            {
                int ChargeId = Convert.ToInt32(this.txtChargeId.Text);
                gcBill.DataSource = busBill.BUS_Bills_GetByChargeId(ChargeId);
            }
        }

        private void chkSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkSelectAll.Checked)
            {
                this.gvBill.SelectAll();
            }
            else
            {
                foreach (int i in gvBill.GetSelectedRows())
                {
                    gvBill.UnselectRow(i);
                }
            }
        }

        private void gvBill_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            this.chkSelectAll.Checked = false;
        }
    }
}